package com.xf.system.utils.redis;

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author xiefengf
 */

@Configuration
public class RedissonConfig {

    @Value("${spring.redis.host}")
    private String host;
    @Value("${spring.redis.port}")
    private String port;
    @Value("${spring.redis.password}")
    private String password;
    @Value("${spring.redis.database}")
    private Integer dataBase;

    /**
     * RedissonClient,单机模式
     * @return
     */
    @Bean(destroyMethod = "shutdown")
    public RedissonClient redisson() {
        Config config = new Config();
        config.useSingleServer()
                .setAddress("redis://" + host + ":" + port)
//                .setPassword(password)
                .setDatabase(dataBase);
        return Redisson.create(config);
    }

    /**
     * Redisson,集群模式
     * @return
     */
//    @Value("${spring.redis.cluster.nodes}")
//    private String host;

//    @Bean(destroyMethod = "shutdown")
//    public RedissonClient redissonClient() {
//        Config config = new Config();
//        //集群模式配置
//        String[] nodes = host.split(",");
//        List<String> clusterNodes = new ArrayList<>();
//        for (int i = 0; i < nodes.length; i++) {
//            clusterNodes.add("redis://" + nodes[i]);
//        }
//        ClusterServersConfig clusterServersConfig = config.useClusterServers()
//                .addNodeAddress(clusterNodes.toArray(new String[clusterNodes.size()]));
//        clusterServersConfig.setPassword(password);
//
//        //看门狗的锁续期时间，默认30000ms，这里配置成15000ms
//        config.setLockWatchdogTimeout(15000);
//        return Redisson.create(config);
//    }
}